#pragma once

#include "../node/node.h"

OPEN_JLIB_NS


struct ListenerQueue {

  static constexpr s32 RenderPriority = 0x7FFFFFFF;

  using handler_t = std::function<bool(Node*)>;

  struct Entry {
    List joint;
    Ptr<Node> node;
    s32 priority;
  };

  bool add (Node *node, s32 priority) noexcept;
  bool remove (Node *node);
  void foreach (const handler_t &handler);
  void clear ();

private:
  std::unordered_map<Node*, Entry> _map;
  List _queue = nullptr;
#ifdef NDEBUG
  int _lock;
#endif
};


CLOSE_JLIB_NS
